/*
 * @(#)clientSever1.java   10/01/28
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package simpleExample;

import fr.ema.arch3d.editor.mainGUI.DrawGraph;
import fr.ema.arch3d.editor.mainGUI.DrawGraph1;
import fr.ema.arch3d.jmodel.assembly.DefaultAssembly;
import fr.ema.arch3d.jmodel.assembly.DefaultComponentInstance;
import fr.ema.arch3d.jmodel.assembly.DefaultConnectorInstance;
import fr.ema.arch3d.jmodel.configuration.Configuration;
import fr.ema.arch3d.jmodel.configuration.DefaultComponentType;
import fr.ema.arch3d.jmodel.configuration.DefaultConfiguration;
import fr.ema.arch3d.jmodel.configuration.DefaultConnectorClass;
import fr.ema.arch3d.jmodel.configuration.DefaultConnectorType;
import fr.ema.arch3d.jmodel.configuration.DefaultPrimitiveComponentClass;
import fr.ema.arch3d.jmodel.interfaceADL.DefaultInterface;
import fr.ema.arch3d.jmodel.specification.DefaultComponentRole;
import fr.ema.arch3d.jmodel.specification.DefaultConnection;
import fr.ema.arch3d.jmodel.specification.DefaultSpecification;
import fr.ema.arch3d.transformer.transformModel.TransformJModelToREF;
import fr.ema.arch3d.transformer.transformModel.TransformJModelToREFImpl;
import fr.ema.arch3d.xoadl.assembly.AssemblyADL;
import fr.ema.arch3d.xoadl.assembly.AssemblyConstraintADL;
import fr.ema.arch3d.xoadl.assembly.ComponentInstanceADL;
import fr.ema.arch3d.xoadl.assembly.ConnectorInstanceADL;
import fr.ema.arch3d.xoadl.assembly.impl.AssemblyADLImpl;
import fr.ema.arch3d.xoadl.configuration.ComponentClassADL;
import fr.ema.arch3d.xoadl.configuration.CompositeComponentClassADL;
import fr.ema.arch3d.xoadl.configuration.ConfigurationADL;
import fr.ema.arch3d.xoadl.configuration.ConnectorClassADL;
import fr.ema.arch3d.xoadl.configuration.PrimitiveComponentClassADL;
import fr.ema.arch3d.xoadl.configuration.impl.ConfigurationADLImpl;
import fr.ema.arch3d.xoadl.interfaceADL.InterfaceADL;
import fr.ema.arch3d.xoadl.specification.ComponentRoleADL;
import fr.ema.arch3d.xoadl.specification.ConnectionADL;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.factory.InstantiationException;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/12/30
 * @author         Huaxi (Yulin) ZHANG
 */
public class clientSever1 {

    /**
     * Method description
     *
     * @param args
     * @throws InstantiationException
     * @throws NoSuchInterfaceException
     */
    public static void main(String[] args) throws InstantiationException, NoSuchInterfaceException {
        InterfaceADL     r1         = new DefaultInterface("r1", "required", "simpleExample.Service");
        InterfaceADL     r          = new DefaultInterface("r", "required", "simpleExample.Service");
        InterfaceADL     r2         = new DefaultInterface("r2", "required", "simpleExample.Service");
        InterfaceADL     p          = new DefaultInterface("p", "provided", "simpleExample.Service");
        InterfaceADL     s2         = new DefaultInterface("s2", "provided", "simpleExample.Go");
        ComponentRoleADL clientRole = new DefaultComponentRole("clientRole", new InterfaceADL[] { r1 },
                                          "!cItf1.findName1*|!cItf2.findName2*");
        ComponentRoleADL server1Role = new DefaultComponentRole("server1Role", new InterfaceADL[] { p },
                                           "?sItf1.findName1*");

        // ComponentRoleADL server2Role=new DefaultComponentRole("server2Role",new InterfaceADL[]{s2},"?sItf2.findName2*");
        ConnectionADL        connection1 = new DefaultConnection("connection1", "clientRole", "r1", "server1Role", "p");
        ConnectionADL        connection2 = new DefaultConnection("connection2", "clientRole", "r1", "server2Role",
                                               "s2");
        DefaultSpecification spec        = new DefaultSpecification("clientServerSpec", new ComponentRoleADL[] { clientRole,
                server1Role }, new ConnectionADL[] { connection1 }, "clientRole.r1.findName?;!serverRole.s1.findNaame", "2.0", "1.0");

//
//      SpecificationBehaviorConsistencyChecker.check(spec);
//      NameConsistencyChecker.check(spec, client);
        DefaultComponentType ctc         = new DefaultComponentType("ct1", new InterfaceADL[] { r1, s2 }, null);
        DefaultComponentType cts         = new DefaultComponentType("ct1", new InterfaceADL[] { p }, null);
        ComponentClassADL    clientClass = new DefaultPrimitiveComponentClass("clientClass", ctc, "2.0", "1.0",
                                               new String[] {}, "simpleExample.ClientImpl");
        ComponentClassADL serverClass = new DefaultPrimitiveComponentClass("serverClass", cts, "2.0", "1.0",
                                            new String[] {}, "simpleExample.ServerImpl");
        ConfigurationADLImpl.ComponentImplementationImpl ci1 = new ConfigurationADLImpl.ComponentImplementationImpl();

        ci1.setComponentClassName("clientClass");
        ci1.setImplComponentRoleName("clientRole");

        ConfigurationADLImpl.ComponentImplementationImpl ci2 = new ConfigurationADLImpl.ComponentImplementationImpl();

        ci2.setComponentClassName("serverClass");
        ci2.setImplComponentRoleName("server1Role");

        DefaultConnectorType connectort1 = new DefaultConnectorType("connectort1", new InterfaceADL[] { r, p }, null);
        ConnectorClassADL    connector1  = new DefaultConnectorClass("connector1", connectort1,
                                               "simpleExample.ConnectorImpl", new String[] {});
        ConfigurationADLImpl.ConnectorImplementationImpl connectori1 =
            new ConfigurationADLImpl.ConnectorImplementationImpl();

        connectori1.setConnectorClassName("connector1");
        connectori1.setImplConnectionName("connection1");

        Configuration config = new DefaultConfiguration("clientServerConfig", spec,
                                   new PrimitiveComponentClassADL[] { (PrimitiveComponentClassADL) clientClass,
                (PrimitiveComponentClassADL) serverClass }, new CompositeComponentClassADL[] {},
                    new ConnectorClassADL[] { connector1 }, new ConfigurationADL.ComponentImplementation[] { ci1,
                ci2 }, new ConfigurationADL.ConnectorImplementation[] { connectori1 });

        System.out.println("edges===" + config.getConfigConnection().get(0).getProvidedCmpName());

        ComponentInstanceADL               clientIns    = new DefaultComponentInstance("clientIns", "clientClass");
        ComponentInstanceADL               serverIns    = new DefaultComponentInstance("serverIns", "serverClass");
        ConnectorInstanceADL               connectorIns = new DefaultConnectorInstance("connectorIns", "connector1");
        AssemblyADL.ComponentInstantiation cci1         = new AssemblyADLImpl.ComponentInstantiationImpl();
        AssemblyADL.ComponentInstantiation cci2         = new AssemblyADLImpl.ComponentInstantiationImpl();

        cci1.setComponentInstanceName("clientIns");
        cci1.setInsComponentRoleName("clientRole");
        cci2.setComponentInstanceName("serverIns");
        cci2.setInsComponentRoleName("server1Role");

        AssemblyADL.ConnectorInstantiation ac = new AssemblyADLImpl.ConnectorInstantiationImpl();

        ac.setConnectorInstanceName("connectorIns");
        ac.setInsConnectionName("connection1");

        DefaultAssembly ass = new DefaultAssembly("clientServerAss", (ConfigurationADLImpl) config,
                                  new ComponentInstanceADL[] { clientIns,
                serverIns }, new ConnectorInstanceADL[] { connectorIns }, new AssemblyConstraintADL[] {},
                             new AssemblyADL.ComponentInstantiation[] { cci1,
                cci2 }, new AssemblyADL.ConnectorInstantiation[] { ac });
        TransformJModelToREF t = new TransformJModelToREFImpl();

        System.out.println("heremain");

        DrawGraph1 dg        = new DrawGraph1();
        Component container = t.transformAssembly(ass);

        dg.drawArch(spec, config, container);
    }
}
